package com.redsoil.house.ui.property.fragment;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ScrollView;

import com.blankj.utilcode.util.EmptyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.redsoil.house.R;
import com.redsoil.house.app.AppContext;
import com.redsoil.house.base.RootFragment;
import com.redsoil.house.model.bean.OnePropertyVO;
import com.redsoil.house.model.bean.PropertyLeaseVO;
import com.redsoil.house.model.bean.TestVO;
import com.redsoil.house.ui.property.activity.PropertyActivity;
import com.redsoil.house.ui.property.adapter.PropertyLeaseAdapter;
import com.redsoil.house.ui.property.contract.PropertyInfoContract;
import com.redsoil.house.ui.property.contract.PropertyLeaseContract;
import com.redsoil.house.ui.property.presenter.PropertyInfoPresenter;
import com.redsoil.house.ui.property.presenter.PropertyLeasePresenter;
import com.redsoil.house.utils.Util;
import com.redsoil.house.widget.PropertyAdView;
import com.redsoil.house.widget.PropertyInfoItem;
import com.zhy.autolayout.AutoLinearLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 物业租约
 */
public class PropertyLeaseFragment extends RootFragment<PropertyLeasePresenter> implements PropertyLeaseContract.View {

    public static PropertyLeaseFragment newInstance(String propertyId) {
        Bundle args = new Bundle();

        PropertyLeaseFragment fragment = new PropertyLeaseFragment();
        fragment.setArguments(args);
        args.putString("propertyId",propertyId);
        return fragment;
    }

    @BindView(R.id.scroll_view)
    ScrollView scroll_view;

    @BindView(R.id.lease_name)
    PropertyInfoItem lease_name;

    @BindView(R.id.lease_age)
    PropertyInfoItem lease_age;

    @BindView(R.id.lease_sex)
    PropertyInfoItem lease_sex;

    @BindView(R.id.lease_peoples)
    PropertyInfoItem lease_peoples;

    @BindView(R.id.lease_rent)
    PropertyInfoItem lease_rent;

    @BindView(R.id.lease_manage_cost)
    PropertyInfoItem lease_manage_cost;

    @BindView(R.id.cash_gift)
    PropertyInfoItem cash_gift;

    @BindView(R.id.deposit)
    PropertyInfoItem deposit;

    @BindView(R.id.bond)
    PropertyInfoItem bond;

    @BindView(R.id.bond_company)
    PropertyInfoItem bond_company;

    @BindView(R.id.bond_people)
    PropertyInfoItem bond_people;

    @BindView(R.id.lease_date)
    PropertyInfoItem lease_date;

    @BindView(R.id.lease_finish_date)
    PropertyInfoItem lease_finish_date;

    @BindView(R.id.lease_free_date)
    PropertyInfoItem lease_free_date;

    @BindView(R.id.ad_view)
    PropertyAdView ad_view;

    @BindView(R.id.property_lease_layout)
    AutoLinearLayout property_lease_layout;

    @BindView(R.id.rv_list)
    RecyclerView rv_list;

    private PropertyLeaseAdapter propertyLeaseAdapter;

    private String propertyId;

    private int showIndex = 0; // 0是公寓 1 是列表 2是内容

    @Override
    protected void initEventAndData() {
        super.initEventAndData();
        propertyId = getArguments().getString("propertyId");

        stateLoading();
        mPresenter.getOneProperty(propertyId);
    }


    @Override
    public void onSupportVisible() {
        super.onSupportVisible();
        if(showIndex == 2 && propertyId != null){
            property_lease_layout.setVisibility(View.VISIBLE);
            scroll_view.setVisibility(View.GONE);
        }
    }

    @Override
    public void getOnePropertySuccess(OnePropertyVO onePropertyVO) {

        //获取基础信息
        if(onePropertyVO.getRooms()!=null && onePropertyVO.getRooms().size() > 0){
            rv_list.addItemDecoration(new DividerItemDecoration(getContext(),DividerItemDecoration.VERTICAL));
            rv_list.setLayoutManager(new LinearLayoutManager(getContext()));

            propertyLeaseAdapter = new PropertyLeaseAdapter(R.layout.property_lease_list_item,onePropertyVO.getRooms());

            propertyLeaseAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    //只有 租赁中能进入查看
//                    state = 2
                    OnePropertyVO.RoomsBean data = (OnePropertyVO.RoomsBean) adapter.getData().get(position);

                    if(data.getState().equals("2")){
                        showIndex = 2;
                        stateLoading();
                        mPresenter.getPropertyLease(data.getId());
                    }
                }
            });

            rv_list.setAdapter(propertyLeaseAdapter);

            showIndex = 1;

            stateMain();

        }else {
            showIndex = 0;
            //请求接口
            mPresenter.getPropertyLease(onePropertyVO.getRoomId());
        }
    }

    @Override
    public void getPropertyLeaseSuccess(PropertyLeaseVO propertyLeaseVO) {

        lease_name.setData(getResources().getString(R.string.name),propertyLeaseVO.getInName(),getResources().getDrawable(R.drawable.shape_top_edge));
        lease_age.setData(getResources().getString(R.string.age),propertyLeaseVO.getAge()+"",getResources().getDrawable(R.drawable.shape_top_edge));
        lease_sex.setData(getResources().getString(R.string.sex),propertyLeaseVO.getSex().equals("0") ? "女" : "男",getResources().getDrawable(R.drawable.shape_top_edge));
        lease_peoples.setData(getResources().getString(R.string.peoples),propertyLeaseVO.getInCount()+"",getResources().getDrawable(R.drawable.shape_top_bottom_edge));

        lease_rent.setData(getResources().getString(R.string.rent),
                Util.nfValue(propertyLeaseVO.getJrent()),
                getResources().getDrawable(R.drawable.shape_top_edge));

        lease_manage_cost.setData(getResources().getString(R.string.lease_manage_cost),
                Util.nfValue(propertyLeaseVO.getJmanageCost()),
                getResources().getDrawable(R.drawable.shape_top_edge));

        cash_gift.setData(getResources().getString(R.string.cash_gift),
                Util.nfValue(propertyLeaseVO.getJcashGift()),
                getResources().getDrawable(R.drawable.shape_top_edge));

        deposit.setData(getResources().getString(R.string.deposit),
                Util.nfValue(propertyLeaseVO.getJcashCost()),
                getResources().getDrawable(R.drawable.shape_top_edge));

        bond.setData(getResources().getString(R.string.bond),
                Util.nfValue(propertyLeaseVO.getJdeposit()),
                getResources().getDrawable(R.drawable.shape_top_edge));

        bond_company.setData(getResources().getString(R.string.bond_company),propertyLeaseVO.getEnsure(),getResources().getDrawable(R.drawable.shape_top_edge));
        bond_people.setData(getResources().getString(R.string.bond_people),propertyLeaseVO.getVoucher(),getResources().getDrawable(R.drawable.shape_top_edge));
        lease_date.setData(getResources().getString(R.string.lease_date),propertyLeaseVO.getStartDate(),getResources().getDrawable(R.drawable.shape_top_edge));
        lease_finish_date.setData(getResources().getString(R.string.lease_finish_date),propertyLeaseVO.getEndDate(),getResources().getDrawable(R.drawable.shape_top_edge));

        if(EmptyUtils.isEmpty(propertyLeaseVO.getRentFree())){
            propertyLeaseVO.setRentFree("无");
        }
        lease_free_date.setData(getResources().getString(R.string.lease_free_date),propertyLeaseVO.getRentFree(),getResources().getDrawable(R.drawable.shape_top_bottom_edge));

//          广告投放
        if(propertyLeaseVO.getAds().size() == 0){
            ad_view.setVisibility(View.GONE);
        }else {
            ad_view.setVisibility(View.VISIBLE);
            ad_view.setData(propertyLeaseVO.getAds());
        }


        property_lease_layout.setVisibility(View.GONE);
        scroll_view.setVisibility(View.VISIBLE);

        stateMain();
    }

    @Override
    public void responeError(String errorMsg) {
//        stateError();
        initErrorView();
    }

    private void initErrorView(){
        lease_name.setData(getResources().getString(R.string.name),"",getResources().getDrawable(R.drawable.shape_top_edge));
        lease_age.setData(getResources().getString(R.string.age),"",getResources().getDrawable(R.drawable.shape_top_edge));
        lease_sex.setData(getResources().getString(R.string.sex),"",getResources().getDrawable(R.drawable.shape_top_edge));
        lease_peoples.setData(getResources().getString(R.string.peoples),"",getResources().getDrawable(R.drawable.shape_top_bottom_edge));

        lease_rent.setData(getResources().getString(R.string.rent),
                "",
                getResources().getDrawable(R.drawable.shape_top_edge));

        lease_manage_cost.setData(getResources().getString(R.string.lease_manage_cost),
                "",
                getResources().getDrawable(R.drawable.shape_top_edge));

        cash_gift.setData(getResources().getString(R.string.cash_gift),
                "",
                getResources().getDrawable(R.drawable.shape_top_edge));

        deposit.setData(getResources().getString(R.string.deposit),
                "",
                getResources().getDrawable(R.drawable.shape_top_edge));

        bond.setData(getResources().getString(R.string.bond),
                "",
                getResources().getDrawable(R.drawable.shape_top_edge));

        bond_company.setData(getResources().getString(R.string.bond_company),"",getResources().getDrawable(R.drawable.shape_top_edge));
        bond_people.setData(getResources().getString(R.string.bond_people),"",getResources().getDrawable(R.drawable.shape_top_edge));
        lease_date.setData(getResources().getString(R.string.lease_date),"",getResources().getDrawable(R.drawable.shape_top_edge));
        lease_finish_date.setData(getResources().getString(R.string.lease_finish_date),"",getResources().getDrawable(R.drawable.shape_top_edge));
        lease_free_date.setData(getResources().getString(R.string.lease_free_date),"",getResources().getDrawable(R.drawable.shape_top_bottom_edge));


        ad_view.setVisibility(View.GONE);


        property_lease_layout.setVisibility(View.GONE);
        scroll_view.setVisibility(View.VISIBLE);

        stateMain();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_property_lease;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }
}
